<?php

namespace App\Listeners;

use App\Constant\QueueKey;
use App\Events\MessageSendEvent;
use App\Models\MessageSended;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;
class MessageSendListener implements ShouldQueue
{
    use InteractsWithQueue;

    public $queue = QueueKey::QUEUE_MESSAGE_SEND;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Description:发送消息时间处理
     * Author: Sam
     * DateTime: 2019/1/11 10:01
     * @param MessageSendEvent $event
     * @throws Exception
     */
    public function handle(MessageSendEvent $event)
    {
        $send = $event->send;
        DB::beginTransaction();
        try{
           $res = MessageSended::create($send);
           DB::commit();
           Log::debug("后台用户【{$send['sender_id']}】发送消息【{$res}】到APP用户【{$send['user_id']}】成功");
        }catch (Exception $exception){
            Log::error("后台用户【{$send['sender_id']}】发送消息到APP用户【{$send['user_id']}】失败:{$exception->getMessage()}");
            DB::rollBack();
        }
    }
}
